package com.woniu.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.dto.ProductInfoDto;
import com.woniu.order.feign.ZsbProductFeign;
import com.woniu.order.pojo.ZcxOrderDetails;
import com.woniu.order.service.ZcxOrderDetailsService;
import com.woniu.order.mapper.ZcxOrderDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 张
* @description 针对表【zcx_order_details】的数据库操作Service实现
* @createDate 2024-11-24 18:45:54
*/
@Service
public class ZcxOrderDetailsServiceImpl extends ServiceImpl<ZcxOrderDetailsMapper, ZcxOrderDetails>
    implements ZcxOrderDetailsService{

    @Autowired
    private  ZcxOrderDetailsMapper zcxOrderDetailsMapper;
    @Autowired
    private ZsbProductFeign zsbProductFeign;
    @Override
    public ResponseData<?> queryOrderDetailByCode(String orderCode) {
        //查询订单详情表，状态最大的订单详情
        List<ZcxOrderDetails> zcxOrderDetails = zcxOrderDetailsMapper.queryOrderDetailByCode(orderCode);
        // 找到最大状态值
        int maxStatus = Integer.MIN_VALUE;
        for (ZcxOrderDetails detail : zcxOrderDetails) {
            if (Integer.parseInt(detail.getStatus())> maxStatus) {
                maxStatus = Integer.parseInt(detail.getStatus());
            }
        }
        // 将状态等于最大值的详情放入集合
        List<ZcxOrderDetails> maxStatusDetails = new ArrayList<>();
        for (ZcxOrderDetails detail : zcxOrderDetails) {
            if (Integer.parseInt(detail.getStatus()) == maxStatus) {
                maxStatusDetails.add(detail);
            }
        }
        //比对数据，更新最新库存
        List<ProductInfoDto> productInfoDtos = zsbProductFeign.queryAllProductList();
        for (ZcxOrderDetails maxStatusDetail : maxStatusDetails) {
            for (ProductInfoDto productInfoDto : productInfoDtos) {
                if (maxStatusDetail.getProductCode().equals(productInfoDto.getProductCode())) {
                    //如果产品相同，则更新库存
                    maxStatusDetail.setInventory(String.valueOf(productInfoDto.getProductStock()));
                }
            }
        }
        return new ResponseData<>().ok(maxStatusDetails);
    }
}




